package com.utils;

import be.ac.ulg.montefiore.run.jahmm.Observation;

import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bigmar
 * Date: 12/9/14
 * Time: 11:24 AM
 * To change this template use File | Settings | File Templates.
 */
public class SequenceIterator implements Iterator<Observation>
{
    List<? extends List<? extends Observation>> seqs;
    int indexOfNextSequence =0;
    Iterator<? extends Observation> currentIterator;
    private int numberOfObservations;

    private int currentObservation=0;
    public SequenceIterator(List<? extends List<? extends Observation>> seqs, int numberOfObservations)
    {
        this.seqs=seqs;
        this.numberOfObservations=numberOfObservations;
    }

    @Override
    public boolean hasNext() {
        return currentObservation<numberOfObservations;
    }
    public boolean isNewSequence()
    {
        return (currentIterator==null||!currentIterator.hasNext());
    }
    public int getCurrentObservation()
    {
        return currentObservation-1;
    }
    @Override
    public Observation next()
    {
        if (!hasNext())
            return null;

        while (currentIterator==null||!currentIterator.hasNext())
        {
            currentIterator=seqs.get(indexOfNextSequence).iterator();
            ++indexOfNextSequence;
        }
        ++currentObservation;
        return currentIterator.next();
    }

    @Override
    @Deprecated
    public void remove()
    {

    }
    public SequenceIterator createNewIterator()
    {
        return new SequenceIterator(seqs, numberOfObservations);
    }
    public void init()
    {
        indexOfNextSequence=0;
        currentObservation=0;
        currentIterator=null;
    }
    public int getNumberOfSequences()
    {
        return seqs.size();
    }
}
